package com.software.modelcenter.controller;

import com.software.modelcenter.domain.Result;
import com.software.modelcenter.entity.Collection;
import com.software.modelcenter.entity.ModelInfo;
import com.software.modelcenter.service.ModelInfoService;
import com.software.modelcenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Author xiaoliao
 * @Date 2023/5/5 8:37
 */
@RestController
@RequestMapping("/model_info")
@RefreshScope
public  class ModelInfoController {

    @Autowired
    private ModelInfoService modelInfoService;


    @GetMapping("/list")
    public List<ModelInfo> findAll() {
        List<ModelInfo> modelInfos = modelInfoService.findAll();
        return modelInfos;
    }
    @GetMapping("/list_page")
    public Page<ModelInfo> findAll(@RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
                                   @RequestParam(name = "pageSize", required = false, defaultValue = "5") String pageSize
    ) {
        Page<ModelInfo> page = modelInfoService.findAll(Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    @GetMapping("/findModelInfosByModelTypeAndModelName")
    public Page<ModelInfo> findModelInfosByModelTypeAndModelName(
                                                     @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
                                                     @RequestParam(name = "pageSize", required = false, defaultValue = "5") String pageSize,
                                                     @RequestParam(name = "modelType", required = false) String modelType,
                                                     @RequestParam(name = "modelName", required = false) String modelName
    ){
        Page<ModelInfo> page = modelInfoService.findModelInfosByModelTypeAndModelName(Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize), modelType, modelName);
        return page;
    }


    @PostMapping("/addModelInfo")
    public Result<ModelInfo> addModelInfo(
            @RequestParam("modelName") String modelName,
            @RequestParam("modelType") String modelType,
            @RequestParam("modelDescription") String modelDescription,
            @RequestParam("keyLine") String keyLine,
            @RequestParam("modelImg") MultipartFile file){
        ModelInfo modelInfo = new ModelInfo();

        modelInfo.setModelId(UUID.randomUUID().toString());
        modelInfo.setModelName(modelName);
        modelInfo.setModelType(modelType);
        modelInfo.setModelDescription(modelDescription);
        modelInfo.setCollectionCount(0);
        modelInfo.setKeyLine(keyLine);

        ModelInfo modelInfo1 = modelInfoService.insertModelInfo(modelInfo, file);
        if(modelInfo1 == null){
            return ResultUtil.error("500","图片上传错误", null);
        }else{
            return ResultUtil.success(modelInfo1);
        }
    }

    //通过typeID删除
    @GetMapping(value = "/deleteModelInfoByModelId/{modelId}")
    public Result<ModelInfo> deleteModelInfoByModelId(@PathVariable String modelId){
        modelInfoService.deleteModelInfoByModelId(modelId);
        return ResultUtil.success(null);
    }

    //修改分类信息
    @PostMapping(value = "/updateModelInfo")
    public Result<ModelInfo> updateModelInfo(
            @RequestParam("modelId") String modelId,
            @RequestParam("modelName") String modelName,
            @RequestParam("modelType") String modelType,
            @RequestParam("modelImg") MultipartFile file,
            @RequestParam("modelDescription") String modelDescription,
            @RequestParam("keyLine") String keyLine,
            @RequestParam("collectionCount") int collectionCount
            ){

        ModelInfo modelInfo = new ModelInfo();
        modelInfo.setModelId(modelId);
        modelInfo.setModelName(modelName);
        modelInfo.setModelType(modelType);
        modelInfo.setModelDescription(modelDescription);
        modelInfo.setCollectionCount(collectionCount);
        modelInfo.setKeyLine(keyLine);
        ModelInfo modelInfo1 = modelInfoService.updateModelInfo(modelInfo, file);
        if(modelInfo1 == null){
            return ResultUtil.error("500","图片上传错误", null);
        }else{
            return ResultUtil.success(modelInfo1);
        }
    }

    /**
     * @param upLoadFile 接收文件
     * @param imgName 接收其余参数
     * @return
     * @throws IOException
     */
    @PostMapping("/recognize")
    public String upload(MultipartFile upLoadFile, String imgName) {
        String sdFile = modelInfoService.recognize(upLoadFile, imgName);
        System.out.println(sdFile);
        return sdFile;
    }

    //通过userId查询全部
    @RequestMapping(value = "/findAllByUserId")
    public List<ModelInfo> findAllByUserId(@RequestBody Collection collection){
        String userId=collection.getUserId();
        List<ModelInfo> list =modelInfoService.findModelByUserId(userId);
        return list;
    }

    //收藏数量+1
    @RequestMapping(value = "/addCount")
    public void addCount(@RequestBody ModelInfo modelInfo){
        modelInfoService.addCount(modelInfo);

    }
    //收藏数量-1
    @RequestMapping(value = "/deCount")
    public void deCount(@RequestBody ModelInfo modelInfo){
        modelInfoService.deCount(modelInfo);
    }

}
